package com.habdqn.attendance.util;

import com.habdqn.attendance.dto.Event;
import com.habdqn.attendance.entity.Record;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 转换数据类型的工具类
 * @author Xing Jiejian 2019-10-21
 */

public class Converter {

    /**
     * 将考勤记录集合转换成前端日期活动集合
     * @param records
     * @return
     */
    public static List<Event> toEventList(List<Record> records){
        List<Event> events = null;
        if(records!=null&&records.size()>0){
            events = new ArrayList<Event>();
            for (Record record: records) {
                // 1.转换上班时间活动
                if(record.getWorkTime()!=null){
                    Event event = new Event();
                    // 日期
                    event.setStart(record.getWorkDay());
                    // 标题
                    event.setTitle("上班：" + dateToString(record.getWorkTime(),"HH:mm"));
                    // 是否迟到
                    if(record.getIsLate()>0){
                        event.setTextColor("red");
                    }
                    events.add(event);
                }
                // 2.转换下班时间活动
                if(record.getOutTime()!=null){
                    Event outTime = new Event();
                    // 日期
                    outTime.setStart(record.getWorkDay());
                    // 标题
                    outTime.setTitle("下班：" + dateToString(record.getOutTime(),"HH:mm"));
                    // 是否迟到
                    if(record.getIsLeaveEarly()>0){
                        outTime.setTextColor("red");
                    }
                    events.add(outTime);
                }
                // 3.是否缺勤
                if(record.getAbsenteeism()>0){
                    Event absenteeism = new Event();
                    // 日期
                    absenteeism.setStart(record.getWorkDay());
                    // 标题
                    absenteeism.setTitle("缺勤");
                    absenteeism.setTextColor("red");
                    events.add(absenteeism);
                }
            }
        }
        return events;
    }

    /**
     * 将日期转换成指定格式字符串
     * @return
     */
    public static String dateToString(Date date,String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
}
